package org.chemodansama.fifteen;

import javax.microedition.khronos.opengles.GL10;

import org.chemodansama.engine.NpActivityTerminator;
import org.chemodansama.engine.NpGame;
import org.chemodansama.engine.NpGameState;
import org.chemodansama.engine.NpSurface;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

public final class Fifteen extends Activity {
    
    private static final String GUI_SLEEKSPACE_MYSCHEME = "gui/sleekspace.myscheme";
    NpSurface mView = null;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
        
        NpGame g = new NpGame(this) {
            @Override
            public NpGameState constructInitialState(NpGame g, GL10 gl, 
                    AssetManager assets) {
                return new StateMenu(g, gl, assets);
//                return new StateGame(g, gl, assets, 3);
            }
        };
        
        mView = new NpSurface(this, new NpActivityTerminator(this), g, 
                              GUI_SLEEKSPACE_MYSCHEME); 
        
        setContentView(mView);
        
        // title bar and notifications bar are not needed
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        // control music volume, not the ring-tone volume
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mView.onResume();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        mView.onPause();
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        
        boolean r = mView.handleKeyEvent(keyCode, event); 
        
        if (!r) {
            r = super.onKeyDown(keyCode, event);
        }
        
        return r;
    }
    
    @Override
    public void onBackPressed() {
        if (!mView.onBackPressed()) {
            super.onBackPressed();
        }
    }
    
}    
